noinst_LTLIBRARIES = libdriver_test.la libsql.la

SQL_DRIVER_PLUGINS =

# automake seems to force making this unconditional..
NOPLUGIN_LDFLAGS =

if SQL_PLUGINS
if BUILD_MYSQL
MYSQL_LIB = libdriver_mysql.la
SQL_DRIVER_PLUGINS += mysql
endif
if BUILD_PGSQL
PGSQL_LIB = libdriver_pgsql.la
SQL_DRIVER_PLUGINS += pgsql
endif
if BUILD_SQLITE
SQLITE_LIB = libdriver_sqlite.la
SQL_DRIVER_PLUGINS += sqlite
endif
if BUILD_CASSANDRA
CASSANDRA_LIB = libdriver_cassandra.la
SQL_DRIVER_PLUGINS += cassandra
endif

sql_module_LTLIBRARIES = \
	$(MYSQL_LIB) \
	$(PGSQL_LIB) \
	$(SQLITE_LIB) \
	$(CASSANDRA_LIB)

sql_moduledir = $(moduledir)
endif

sql_drivers = @sql_drivers@

AM_CPPFLAGS = \
	-I$(top_srcdir)/src/lib \
	-I$(top_srcdir)/src/lib-test \
	-I$(top_srcdir)/src/lib-settings \
	-I$(top_srcdir)/src/lib-ssl-iostream \
	-I$(top_srcdir)/src/lib-var-expand \
	$(SQL_CFLAGS)

dist_sources = \
	sql-api.c

if ! SQL_PLUGINS
driver_sources = \
	driver-mysql.c \
	driver-pgsql.c \
	driver-sqlite.c \
	driver-cassandra.c
endif

libsql_la_SOURCES = \
	$(dist_sources) \
	$(driver_sources) \
	driver-sqlpool.c
libsql_la_LIBADD = $(SQL_LIBS)

nodist_libsql_la_SOURCES = sql-drivers-register.c

if SQL_PLUGINS
libdriver_mysql_la_LDFLAGS = -module -avoid-version
libdriver_mysql_la_LIBADD = $(MYSQL_LIBS)
libdriver_mysql_la_CPPFLAGS = $(AM_CPPFLAGS) $(MYSQL_CFLAGS)
libdriver_mysql_la_SOURCES = driver-mysql.c

libdriver_pgsql_la_LDFLAGS = -module -avoid-version
libdriver_pgsql_la_LIBADD = $(PGSQL_LIBS)
libdriver_pgsql_la_CPPFLAGS = $(AM_CPPFLAGS) $(PGSQL_CFLAGS)
libdriver_pgsql_la_SOURCES = driver-pgsql.c

libdriver_sqlite_la_LDFLAGS = -module -avoid-version
libdriver_sqlite_la_LIBADD = $(SQLITE_LIBS)
libdriver_sqlite_la_CPPFLAGS = $(AM_CPPFLAGS) $(SQLITE_CFLAGS)
libdriver_sqlite_la_SOURCES = driver-sqlite.c

libdriver_cassandra_la_LDFLAGS = -module -avoid-version
libdriver_cassandra_la_LIBADD = $(CASSANDRA_LIBS)
libdriver_cassandra_la_CPPFLAGS = $(AM_CPPFLAGS) $(CASSANDRA_CFLAGS)
libdriver_cassandra_la_SOURCES = driver-cassandra.c
else
endif

libdriver_test_la_LDFLAGS = -avoid-version
libdriver_test_la_CPPFLAGS = $(AM_CPPFLAGS) \
	-I$(top_srcdir)/src/lib-test
libdriver_test_la_SOURCES = driver-test.c

noinst_HEADERS = driver-test.h

pkglib_LTLIBRARIES = libdovecot-sql.la
libdovecot_sql_la_SOURCES =
libdovecot_sql_la_LIBADD = libsql.la $(LIBDOVECOT)
libdovecot_sql_la_DEPENDENCIES = libsql.la $(LIBDOVECOT_DEPS)
libdovecot_sql_la_LDFLAGS = -export-dynamic

headers = \
	sql-api.h \
	sql-api-private.h

pkginc_libdir=$(pkgincludedir)
pkginc_lib_HEADERS = $(headers)

BUILT_SOURCES = sql-drivers-register.c
CLEANFILES = sql-drivers-register.c

sql-drivers-register.c: $(top_builddir)/config.h Makefile.am
	$(AM_V_GEN)rm -f $@; \
	echo '/* this file automatically generated by Makefile */' >$@; \
	echo '#include "lib.h"' >>$@; \
	echo '#include "sql-api-private.h"' >>$@
	$(AM_V_at)echo 'void sql_drivers_init_all(void) {' >>$@
if ! SQL_PLUGINS
	$(AM_V_at)for i in $(sql_drivers) null; do \
	  if [ "$${i}" != "null" ]; then \
	        echo "driver_$${i}_init();" >>$@ ; \
	  fi; \
	done
endif
	$(AM_V_at)echo '}' >>$@
	$(AM_V_at)echo 'void sql_drivers_deinit_all(void) {' >>$@
if ! SQL_PLUGINS
	$(AM_V_at)for i in $(sql_drivers) null; do \
	  if [ "$${i}" != "null" ]; then \
	        echo "driver_$${i}_deinit();" >>$@ ; \
	  fi; \
	done
endif
	$(AM_V_at)echo '}' >>$@

if SQL_PLUGINS
install-exec-local:
	for d in auth dict; do \
	  $(mkdir_p) $(DESTDIR)$(moduledir)/$$d; \
	  for driver in $(SQL_DRIVER_PLUGINS); do \
	    rm -f $(DESTDIR)$(moduledir)/$$d/libdriver_$$driver.so; \
	    $(LN_S) ../libdriver_$$driver.so $(DESTDIR)$(moduledir)/$$d; \
	  done; \
	done
endif

test_libs = \
	$(noinst_LTLIBRARIES) \
	../lib-dovecot/libdovecot.la

check_PROGRAMS = test-sql

test_sql_SOURCES = test-sql.c
test_sql_LDADD =  $(test_libs) $(DLLIB)
test_sql_DEPENDENCIES = $(test_libs)

if BUILD_SQLITE
check_PROGRAMS += test-sql-sqlite
test_sql_sqlite_SOURCES = test-sql-sqlite.c
test_sql_sqlite_LDADD = $(SQLITE_LIB) $(pkglib_LTLIBRARIES) $(test_libs) $(DLLIB)
test_sql_sqlite_DEPENDENCIES = $(SQLITE_LIB) $(pkglib_LTLIBRARIES) $(test_libs)
endif

noinst_PROGRAMS = $(check_PROGRAMS)

check-local:
	for bin in $(check_PROGRAMS); do \
	  if ! $(RUN_TEST) ./$$bin; then exit 1; fi; \
	done
